package com.ciwong.epaper.modules.epaper.ui.newlistenspeak.shortcoversation.fragment;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.dbbean.LswAnswer;
import com.ciwong.epaper.modules.epaper.bean.ResourceDetail;
import com.ciwong.epaper.modules.epaper.ui.newlistenspeak.BaseConversationActivity;
import com.ciwong.libs.audio.play.AudioPlayer;
import com.ciwong.libs.audio.play.OnPlayListener;
import com.ciwong.libs.utils.CWLog;

import java.util.ArrayList;
import java.util.List;

/**
 * date :2018/5/3
 * author: loco
 * description:
 * version:
 * email: lzh838330255@163.com
 */

public abstract class BaseConversationFragment extends Fragment {

    protected String                   TAG                   = getClass().getSimpleName();
    protected Context                  mContext              = null;
    protected BaseConversationActivity mActivity             = null;
    protected boolean                  isStopHandler         = false;// handler是否停止
    protected boolean                  isStopFlag            = false;// 区分是否是手动停止语音播放
    protected ResourceDetail           mResourceDetail       = null;
    protected int mLsMode;
    protected int mBigQuesIndex = 0;
    protected int totalQuesIndex;
    protected String mUuid;
    protected List<ResourceDetail> mIsDoRecords;
    protected ArrayList<LswAnswer> mLswAnswerList = null;//答案集合
    protected ArrayList<LswAnswer> mHasDoneAnswers = null;//答案集合（从上个页面传过来的答案）
    protected boolean mIsPlayStartExamAduio;
    protected boolean isShowTime = true; //间隔时间内不需要显示倒计时
    protected int mTotalCount = 0;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = getContext();
        mActivity = (BaseConversationActivity) getActivity();

        mResourceDetail = mActivity.mResourceDetail;
        mLsMode = mActivity.mLsMode;
        mBigQuesIndex = mActivity.mBigQuesIndex;
        totalQuesIndex = mActivity.mQuesTotalIndex;
        mUuid = mActivity.mUuid;
        mLswAnswerList = mActivity.mLswAnswerList;
        mHasDoneAnswers = mActivity.mHasDoneAnswers;
        mIsDoRecords = mActivity.mIsDoRecords;
        mIsPlayStartExamAduio = mActivity.mIsPlayStartExamAduio;
        isShowTime = mActivity.isShowTime;
        mTotalCount = mActivity.mTotalCount;
    }

    /**
     * 开始播放音频文件(流程开始入口)
     */
    protected void initListener(View view) {
        //先注册播放器监听
        AudioPlayer.getInstance().setOnPlayListener(playListener);
        //是否跳过
        view.setOnClickListener(mSkipListener);
    }

    protected abstract void onSkipClick();

    protected abstract void onHandleMessage(int msgType);

    protected abstract void onPlayerStop(Object audioUrl);

    protected abstract void onPlayerPlaying(long duration, long progress);

    protected abstract void onPlayerError(int arg0, Object arg1);

    View.OnClickListener mSkipListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onSkipClick();
        }
    };

    //计时
    protected Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (mActivity.isFinishing()) {
                return true;
            }
            if (isStopHandler) {
                isStopHandler = false;
                return true;
            }
            int msgType = msg.what;
            onHandleMessage(msgType);
            return true;
        }
    });


    //播放器监听
    private OnPlayListener playListener = new OnPlayListener() {
        @Override
        public void stop(Object audioUrl) {
            if (mActivity.isFinishing()||mActivity.isStopHandle) {
                return;
            }
            if (isStopFlag) {
                isStopFlag = false;
                return;
            }
            onPlayerStop(audioUrl);
        }

        @Override
        public void onPlaying(long duration, long mProgress) {
            if (mActivity.isFinishing()) {
                return;
            }
            onPlayerPlaying(duration, mProgress);
        }

        @Override
        public void onError(int arg0, Object arg1) {
            if (mActivity.isFinishing()) {
                return;
            }
            mActivity.showToastError(R.string.play_audio_fail);
            CWLog.d(TAG, "playListener onError errcode:" + arg0 + ",object:" + arg1);
            playListener.stop(arg1);
            onPlayerError(arg0, arg1);
        }

        @Override
        public void onBuffer(Object arg0, long arg1, long arg2) {
        }

        @Override
        public void onPlayStart(Object arg0) {
        }

        @Override
        public void onReadPlayer(long arg0, Object arg1) {
        }
    };

    /**
     * 移除Handler消息
     *
     * @param what
     */
    protected void removeHandlerMsg(int what, Handler mHandler) {
        if (mHandler.hasMessages(what)) {
            mHandler.removeMessages(what);
        }
    }

}
